#!/usr/bin/env python
# coding=utf-8
# Cron Job 列表

import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.api import users
from google.appengine.ext.webapp import util
import logdb
import common

class LogListHandler(webapp.RequestHandler):
	def get(self):
		user = users.get_current_user()
		if user:
			if users.is_current_user_admin():
				self.response.headers["Content-Type"] = "text/html; charset=utf-8"
				self.writeHtmlHeader()
				self.writeLoopHeader()
				logs = logdb.listLogInfo()
				if logs:
					for log in logs:
						self.writeLoopItem(log)
				self.writeLoopFooter(logdb.get_count())
			else:
				common.writeNoPermissionMessage(self)
		else:
			self.redirect(users.create_login_url(self.request.uri))

	def writeLoopItem(self, log):
		id = log.key().id()
		cronID = log.cronID
		content = log.content
		date = log.date
		self.response.out.write("<tr>")
		self.response.out.write("<td class=\"first-cell\" style=\"text-align:center;\">")
		self.response.out.write("<span>")
		self.response.out.write("%d</span></td>" % id)
		self.response.out.write("<td class=\"first-cell\" style=\"text-align:left;\">")
		self.response.out.write("<span>")
		self.response.out.write(cronID)
		self.response.out.write("</span>")
		self.response.out.write("</td>")
		self.response.out.write("<td class=\"first-cell\" style=\"text-align:left;\">")
		self.response.out.write("<span>")
		self.response.out.write(common.htmlEncode(content))
		self.response.out.write("</span>")
		self.response.out.write("</td>")
		self.response.out.write("<td class=\"first-cell\" style=\"text-align:left;\">")
		self.response.out.write("<span>")
		self.response.out.write(date)
		self.response.out.write("</span>")
		self.response.out.write("</td>")
		self.response.out.write("</tr>")

	def writeLoopHeader(self):
		self.response.out.write("""<body>
<div>
<h1>
	<span class="action-span"><a href="cronlist">Cron Job List</a></span><span class="action-span1"><a
		href="main">Beikx Cron</a> - Run Log </span>
</h1>
<div style="clear: both" />""")
		self.response.out.write("<form id=\"listForm\" action=\"/clearlog\" method=\"post\">")
		self.response.out.write("""
	<div class="list-div" id="listDiv">
	<table cellspacing="1" cellpadding="3" id="list-table">
	<tr>
		<th style="width:95px;">
			Log ID</th>
		<th>
			Cron Job ID</th>
		<th>
			Content</th>
		<th>
			Date Time</th>
	</tr>""");

	def writeLoopFooter(self, count):
		self.response.out.write("""<tr>
	<td colspan="4">
		<input type="button" id="btnClearLog" name="btnClearLog" value="Clear Log" CssClass="button" onclick="javascript: return clearLog();" />&nbsp;&nbsp;Count: %d</td>
</tr>
</table>
</div>
</form>
<div id="footer">
	<br />
	%s
</div>
</div>""" % (count, common.getCopyRight()))
		self.response.out.write(self.getFooterJs())
		self.response.out.write("</body></html>")

	def writeHtmlHeader(self):
		self.response.out.write("""<html>
<head>
    <title>%s</title>
    <link href="css/general.css" rel="stylesheet" type="text/css" />
    <link href="css/main.css" rel="stylesheet" type="text/css" />
    <script src="js/global.js" type="text/javascript" language="javascript"> </script>
</head>""" % common.getAppName())

	def getFooterJs(self):
		return """<script type="text/javascript" language="javascript">
<!-- 
function clearLog() {
	if (confirm('Are you sure to clear?')) {
		window.location = 'clearlog'
	}
	return false;
}
//-->
</script>"""

class ClearLogHandler(webapp.RequestHandler):
	def process(self):
		user = users.get_current_user()
		if user:
			if users.is_current_user_admin():
				logdb.clearLog()
				self.redirect('loglist')
			else:
				common.writeNoPermissionMessage(self)
		else:
			self.redirect(users.create_login_url(self.request.uri))

	def get(self):
		self.process()

	def post(self):
		self.process()

def main():
	application = webapp.WSGIApplication([('/loglist', LogListHandler),('/clearlog', ClearLogHandler)],
		debug=True)
	util.run_wsgi_app(application)


if __name__ == '__main__':
	main()
